<template>
  <div v-loading = "loading">
    <h1>审核等级管理</h1>
    <div style="width:20%;float:left;margin-right:10px">
      <div style="height:25px">
        <el-button size="mini" @click="addLevel" style="float:left;margin-bottom:2px" type="primary" icon="el-icon-plus">新增审核等级</el-button>
      </div>
      <div style="margin-top:5px">
        <el-table :data="tableData"  border v-loading="tableLoading" @row-click="getExaminedByAL" :cell-style="cellStyle" size="mini" style="margin-top:10px"> 
          <el-table-column prop="audit_level_id" label="等级id" align="center" v-if="false"></el-table-column>
          <el-table-column prop="audit_level" label="审核等级" :formatter="showAL" align="center"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="deleteAL(scope.row)" type="danger" icon="el-icon-delete">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div style="width:75%;float:left">
      <div style="height:25px">
        <el-button size="mini" @click="addAR" style="float:left;margin-bottom:2px" type="primary" icon="el-icon-plus">添加审核人</el-button>
      </div>
      <div style="margin-top:5px">
        <el-table :data="tableData1"  border v-loading="tableLoading1" size="mini" style="margin-top:10px">
          <el-table-column prop="ar_order" label="审核人排序"></el-table-column>
          <el-table-column prop="user_name" label="审核人名称"></el-table-column>
          <el-table-column prop="ar_id" label="id" v-if="false"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="deleteAR(scope.row)" type="danger" icon="el-icon-delete">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div>
      <el-dialog title="新增审核等级"
          :visible.sync="dialogVisible"
          width="25%" float="left">
      
        <div>
          <span style="color:red">*</span><span>审核等级</span>
          <el-input v-model="level" style="width: 200px" size="mini" type="number"></el-input>
        </div>
        <div v-if="isMsg1">
          <span style="color:red;font-size:12px">{{msg1}}</span>
        </div>
        <div style="margin-top: 10px">
          <el-button @click="save" size="small" type="primary">保存</el-button>
          <el-button @click="close" size="small" type="danger">关闭</el-button>
        </div>
      </el-dialog>
    </div>
    <div>
      <el-dialog title="添加审核人"
          :visible.sync="dialogVisible1"
          width="25%" float="left">
      <div>
        <span style="color:red">*</span><span>审核人名称</span>
        <el-select v-model="name" style="width: 200px" placeholder="请选择" size="mini" @change="userChange">
              <el-option
                v-for="item in allUser"
                :key="item.audit_manager_id"
                :label="item.user_name"
                :value="item.audit_manager_id">
              </el-option>
            </el-select>
      </div>    
      <div style="margin-top:10px">
        <span style="color:red">*</span><span>审核人顺序</span>
        <el-input v-model="ar_order" style="width: 200px" size="mini" type="number"></el-input>
      </div>
      <div v-if="isMsg2">
          <span style="color:red;font-size:12px">{{msg2}}</span>
      </div>
      <div style="margin-top: 10px">
        <el-button @click="saveAR" size="small" type="primary">保存</el-button>
        <el-button @click="close" size="small" type="danger">关闭</el-button>
      </div>
    </el-dialog>
    </div>
  </div>
</template>
<script>
  export default{
    data(){
      return{
        isMsg1:false,
        msg1:'',
        isMsg2:false,
        msg2:'',
        radio:'',
        tableLoading1:false,
        tableLoading:false,
        dialogVisible:false,
        tableData:[],
        tableData1:[],
        level:'',
        rowid:'',
        dialogVisible1:false,
        ar_order:'',
        allUser:[],
        name:'',
        selectRow:'',
        persons:'',
        loading:false,
      }
    },
    mounted:function(){
      this.tableLoading = true;
      this.loadTableData();
    },
     methods: {
       loadTableData(){
         var _this = this;
          this.getRequest("/auditLevel/getAL").then(resp=>{
            _this.tableLoading=false;
            if (resp && resp.status == 200) {
              _this.tableData = resp.data;
            }
          })
       },
       getExaminedByAL(row,event, column){
         this.selectRow = row;
         this.rowid = row.audit_level_id;
         this.tableLoading1 = true;
         var _this = this;
         this.postRequest("/auditLevel/getExaminedByAL",{
           audit_level_id:row.audit_level_id
         }).then(resp => {
           _this.tableLoading1 = false;
           if (resp && resp.status == 200) {
             _this.persons = resp.data.length;
            _this.tableData1 = resp.data;
           }
         })
       },
       addLevel(){
         this.dialogVisible=true;
       },
       save(){
         if(!this.level){
           this.msg1='请填写审核等级'
           this.isMsg1=true;
           return
         }
         var key = false;
         this.tableData.forEach(item=>{
           
           if(item.audit_level==this.level){
            
             key = true;
           }
         })
         if(key){
            this.$message.warning('当前审核等级已存在')
            return
         }
         var _this = this;
         for(var i=0;i<this.tableData.length;i++){
          // if(this.level==this.tableData.audit_level){
          //   this.$message({type:'error',message:'当前审核等级已存在'})
          // }
         }
         this.loading = true;
         this.postRequest("/auditLevel/addAL",{
           level : this.level
         }).then(resp=>{
           this.loading = false;
          if (resp && resp.status == 200) {
              var data1 = resp.data;
              this.$message({type:data1.status,message:data1.msg});
              this.loadTableData();
              this.close();
              this.rowid=''
          }
         });
       },
       close(){
         this.isMsg1=false;
         this.isMsg2=false;
         this.dialogVisible=false;
         this.dialogVisible1=false;
         this.name=''
         this.ar_order=''
         this.level=''
       },
       addAR(){
         if(this.rowid==''){
           this.$message({type:'error',message:'请选择审核等级'});
           return
         }
          this.dialogVisible1=true;
          this.loaduser();
       },
       loaduser(){
          var _this = this;
          this.loading = true;
          this.postRequest("/auditLevel/getExamined").then(resp=>{
            this.loading = false;
          if (resp && resp.status == 200) {
            _this.allUser = resp.data;
          }
        });
       },
       cellStyle({row, column, rowIndex, columnIndex}){
         if(row.audit_level_id==this.rowid){
           return 'background:gray'
         }else{
           return ''
         }
       },
       userChange(){

       },
       saveAR(){
         if(!this.name){
           this.msg2='请选择一个审核人';
           this.isMsg2=true;
           return
         }
         if(!this.ar_order){
           this.msg2='请输入排序';
           this.isMsg2=true;
           return
         }
         if(this.ar_order>this.selectRow.audit_level){
           this.$message({type:'warning',message:'当前审核顺序不能大于审核等级'});
           return
         }
         
         var _this = this;
         this.loading = true;
         this.postRequest("/auditLevel/addAR",{
           audit_level_id:this.rowid,
           ar_order:this.ar_order,
           audit_manager_id:this.name
         }).then(resp=>{
           this.loading = false;
          if (resp && resp.status == 200) {
              var data1 = resp.data;
              this.$message({type:data1.status,message:data1.msg});
              this.getExaminedByAL(_this.selectRow);
              this.close();
          }
         });
       },
       deleteAL(data){

        this.$confirm('确认删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
              this.loading = true;
                this.postRequest("/auditLevel/deleteAL",{
                  audit_level_id:data.audit_level_id
                }).then(resp=>{
                  this.loading = false;
                    var data1 = resp.data;
                    this.$message({type:data1.status,message:data1.msg});
                    this.loadTableData();
                    this.tableData1=[];
                    this.rowid=''
                    console.log(this.rowid)
                });
            })
         
       },
       deleteAR(data){
          var _this=this
          this.$confirm('确认删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
              this.loading = true;
               this.postRequest("/auditLevel/deleteAR",{
                ar_id:data.ar_id
              }).then(resp=>{
                this.loading = false;
                  var data1 = resp.data;
                  this.$message({type:data1.status,message:data1.msg});
                  this.getExaminedByAL(_this.selectRow);
              });
            })
       },
       showAL(row,column){
         switch(row.audit_level){
            case row.audit_level:
            return row.audit_level+"级审核"
         }
       }
     }
  }
</script>